import { createRouter, createWebHashHistory } from 'vue-router'
import { getToken } from '@/utils/token.js'

const asyncRoutes = [

    {
        path: '/',
        redirect: '/home'
    },
    {
        path: '/home',
        name: 'home',
        component: () => import('@/views/Home/Home.vue'),
        meta: {
            isShowNav: true
        }
    },
    {
        path: '/recommend',
        name: 'recommend',
        component: () => import('@/views/Recommend/Recommend.vue'),
        meta: {
            isShowNav: true,
            needLogin: true
        }
    },
    {
        path: '/cart',
        name: 'cart',
        component: () => import('@/views/Cart/Cart.vue'),
        meta: {
            isShowNav: true,
            titleNav: '购物车'
        }
    },
    {
        path: '/user',
        name: 'user',
        component: () => import('@/views/User/User.vue'),
        meta: {
            isShowNav: true,
            titleNav: '我的'
        }
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('@/views/Login/Login.vue'),
        meta: {
            isShowNav: false,
        }
    },
    {
        // 动态路由
        path: '/goods/:id',
        name: 'goods',
        component: () => import('@/views/Home/GoodsDetail.vue'),
        meta: {
            titleNav: '商品详情'
        }
    },
    {
        path: '/address-list',
        name: 'addressList',
        component: () => import('@/views/Address/Address.vue'),
        meta: {
            login: true, // 访问是否需要登录
            titleNav: '收货地址'   // 顶部标题
        }
    },
    {
        path: '/edit-address/',
        name: 'editAddress',
        component: () => import('@/views/Address/EditAddress.vue'),
        meta: {
            login: true, // 访问是否需要登录
        }
    },
    {
        path: '/create-order/',
        name: 'createOrder',
        component: () => import('@/views/Order/Created.vue'),
        meta: {
            titleNav: '生成订单',
            login: true, // 访问是否需要登录
        }
    },
    {
        path: '/order',
        name: 'order',
        component: () => import('@/views/Order/List.vue'),
        meta: {
            titleNav: '我的订单',
            login: true,
        },
    },
    {
        path: '/add-article',
        name: 'addArticle',
        component: () => import('@/views/Recommend/Add.vue'),
        meta: {
            titleNav: '新增种草文章',
            login: true,
        },
    },
    {
        path: '/article-detail',
        name: 'articleDetail',
        component: () => import('@/views/Recommend/Detail.vue'),
        meta: {
            titleNav: '种草文章详情',
            login: true,
        },
    },
    {
        path: '/my-praise',
        name: 'myPraise',
        component: () => import('@/views/User/MyPraise.vue'),
        meta: {
            titleNav: '我的点赞',
            login: true,
        },
    },
    {
        path: '/my-collect',
        name: 'myCollect',
        component: () => import('@/views/User/MyCollect.vue'),
        meta: {
            titleNav: '我的收藏',
            login: true,
        },
    },
    {
        path: '/edit-password',
        name: 'editPassword',
        component: () => import('@/views/User/EditPassword.vue'),
        meta: {
            titleNav: '修改密码',
            login: true,
        },
    },
];
const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        ...asyncRoutes,
        // { path: "/:pathMatch(.*)", redirect: "/home" }
        // 访问没有的路由直接跳往首页
        {
            path: '/:toHome*',
            name: 'toHome',
            redirect: '/home',
        },
    ]
})
// 全局路由守卫
router.beforeEach((to, from, next) => {
    // 先判断是否登录
    if (getToken()) {
        // 如果已经登录了
        if (to.path === "/login") {
            // 已经登录了 还要去login页面 就从哪来回哪去
            next(from.path)
        } else {
            next();
        }
    } else {
        // 如果未登录
        if (to.meta.needLogin) {
            // to.meta.needLogin 表示对应路由是否需要登录
            next('/login')
        } else {
            next()
        }
    }
})

export default router;